﻿using System;
using System.Data;
using ltModel;

public partial class ltMan_ads_floatAd : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sAds.DataSource = SqlHelper.Instance.ExecuteReader("select * from lt_piao");
            sAds.DataBind();
            LoadData(GetId());
        }
    }

    string GetId()
    {
        string id = Request.QueryString["id"];
        if (!Str.IsInt(id))
            id = "1";
        return id;
    }

    void LoadData(string id)
    {
        string sql = "select * from lt_piao where [id]=" + id;
        using (IDataReader dr = SqlHelper.Instance.ExecuteReader(sql))
        {
            if (dr.Read())
            {
                sFileHid.Value = sImg.Src = dr["pic"].ToString();
                if (!string.IsNullOrEmpty(sImg.Src))
                    sImg.Visible = true;
                sLink.Text = dr["link"].ToString();
                sShow.SelectedToggleStateIndex = Convert.ToInt32(dr["zt"]);
                sBegin.DbSelectedDate = Str.ParseDbTime(dr["stime"]);
                sEnd.DbSelectedDate = Str.ParseDbTime(dr["etime"]);
            }
        }
    }

    protected string Highlight(object oid)
    {
        return Convert.ToInt32(GetId()) == Convert.ToInt32(oid) ? " style=\"color:red\"" : string.Empty;
    }

    protected void RadSave_Click(object sender, EventArgs e)
    {
        string id = GetId();
        string file = sFile.SaveFiles();
        if (!string.IsNullOrEmpty(file))
            sImg.Src = file;
        else
            file = sFileHid.Value;
        string lk = DataProtector.ToSBC(sLink.Text);
        string zt = sShow.SelectedToggleStateIndex.ToString();
        DateTime st = Str.ParseDbTime(sBegin.SelectedDate);
        DateTime et = Str.ParseDbTime(sEnd.SelectedDate);
        IDataParameter[] parms = new IDataParameter[]{
            SqlParameterEx.CreateParameter("@pic", SqlDbType.VarChar, 255, file),
            SqlParameterEx.CreateParameter("@link", SqlDbType.VarChar, 255, lk),
            SqlParameterEx.CreateParameter("@zt", SqlDbType.Int, zt),
            SqlParameterEx.CreateParameter("@st", SqlDbType.SmallDateTime, st),
            SqlParameterEx.CreateParameter("@et", SqlDbType.SmallDateTime, et),
            SqlParameterEx.CreateParameter("@id", SqlDbType.Int, id)
        };
        SqlHelper.Instance.ExecuteNonQuery("update lt_piao set pic=@pic,link=@link,zt=@zt,stime=@st,etime=@et where [id]=@id", parms);
        infoPanel.AddSuccessInfo("修改成功");
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "修改飘浮窗口");
        LoadData(id);
    }
}